from enumobjects.general_enum import YesOrNoEnum


class RoleTree(object):
    def __init__(self, db):
        self.db = db

    def generate_tree(self, menu_list, per_dict):
        lists = []
        if not len(menu_list):
            return lists
        res_dict = {}
        for i in menu_list:
            res_dict[i["mid"]] = i

        for item in menu_list:
            pid = item["parent_id"]

            if per_dict.get(item["mid"]):
                item["pson"] = per_dict[item["mid"]]
            if not pid:
                lists.append(item)
            else:
                if "child" not in res_dict[pid]:
                    res_dict[pid]["child"] = []
                res_dict[pid]["child"].append(item)

        return lists

    def sort_tree(self, per_list):
        id_dict = {}
        for ind, p in enumerate(per_list):
            id_dict[p[0]] = []
        for p in per_list:
            id_dict[p[0]].append({"pid": p[1], "pname": p[2]})
        return id_dict

    def dg_menu(self, menu_info_list, menu_id, menu):
        menu_list = []
        if menu_id:
            menu_info = self.db.query(menu.id,
                                      menu.name,
                                      menu.url,
                                      menu.parent_id).filter(menu.id.in_(menu_id),
                                                             menu.is_delete == YesOrNoEnum.NO.value).all()
            for i in menu_info:
                menu_list.append(i)
            for j in menu_info_list:
                menu_list.append(j)
            menu_id = [i[3] for i in menu_info if i[3] != 0]
            return self.dg_menu(menu_list, menu_id, menu)
        else:
            for j in menu_info_list:
                menu_list.append(j)
            return menu_list

    def tree(self, uid, role_per, role_user, menu, menu_per, per, role):
        session = self.db
        per_list = session.query(role_per.permission_id
                                 ).join(role_user,
                                        role_user.role_id == role_per.role_id) \
            .join(role,
                  role.id == role_per.role_id) \
            .filter(role_user.user_id == uid,
                    role.is_disable == YesOrNoEnum.NO.value,
                    role_user.is_delete == YesOrNoEnum.NO.value,
                    role_per.is_delete == YesOrNoEnum.NO.value).all()
        per_list = [i[0] for i in per_list]

        # 查找权限相同的页面
        menu_query = session.query(menu.id,
                                   menu.name,
                                   menu.url,
                                   menu.parent_id
                                   ).join(menu_per,
                                          menu_per.menu_id == menu.id) \
            .filter(menu_per.permission_id.in_(per_list),
                    menu_per.is_delete == YesOrNoEnum.NO.value,
                    menu.is_delete == YesOrNoEnum.NO.value)

        menu_list = menu_query.all()

        # 菜单的parent_id
        menu_parent_id_list = [i[3] for i in menu_list if i[3] != 0]

        menu_info = self.dg_menu([], menu_parent_id_list, menu)

        menu_all = menu_info + menu_list

        menu_all = list(set(menu_all))
        menu_all_list = [{"mid": i[0], "name": i[1], "url": i[2], "parent_id": i[3]}
                         for i in menu_all]
        menu_id_list = [j[0] for j in menu_all if j[3] != 0]

        permission_list = session.query(menu_per.menu_id,
                                        per.id,
                                        per.name
                                        ).join(menu_per,
                                               menu_per.permission_id == per.id) \
            .filter(menu_per.menu_id.in_(menu_id_list),
                    menu_per.permission_id.in_(per_list),
                    menu_per.is_delete == YesOrNoEnum.NO.value,
                    per.is_delete == YesOrNoEnum.NO.value).all()

        data = self.sort_tree(permission_list)
        res = self.generate_tree(menu_all_list, data)

        return res
